package me.zxk.qwert_poetry.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.zxk.qwert_poetry.dao.Author;
import me.zxk.qwert_poetry.dto.PageResult;
import me.zxk.qwert_poetry.dto.request.AuthorIdDTO;
import me.zxk.qwert_poetry.dto.response.Response;
import me.zxk.qwert_poetry.service.AuthorService;
import me.zxk.qwert_poetry.util.VerifyToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@Tag(name = "作者接口")
@RestController
public class AuthorController {
    @Autowired
    private AuthorService authorService;

    @Operation(summary = "根据 ID 获取作者")
    @GetMapping("/api/author/get/byId")
    public Response<Author> getAuthorById(@RequestParam int id) {
        return Response.ok(authorService.getAuthorById(id));
    }

    @Operation(summary = "根据作者名获取作者")
    @GetMapping("/api/author/get/byName")
    public Response<Author> getAuthorByName(@RequestParam String name) {
        return Response.ok(authorService.getAuthorByName(name));
    }

    @Operation(summary = "添加作者", description = "作者 ID 字段将会被忽略。")
    @PostMapping("/api/author/add")
    @VerifyToken(needAdmin = true)
    public Response<Author> addAuthor(@RequestBody Author author) {
        return Response.ok(authorService.addAuthor(author));
    }

    @Operation(summary = "根据 ID 删除作者")
    @PostMapping("/api/author/delete/byId")
    @VerifyToken(needAdmin = true)
    public Response<Author> deleteAuthorById(@RequestBody AuthorIdDTO dto) {
        return Response.ok(authorService.deleteAuthorById(dto.getId()));
    }

    @Operation(summary = "更新作者")
    @PostMapping("/api/author/edit")
    @VerifyToken(needAdmin = true)
    public Response<Author> updateAuthor(@RequestBody Author author) {
        return Response.ok(authorService.updateAuthor(author));
    }

    @Operation(summary = "列出所有作者")
    @GetMapping("/api/author/list")
    public Response<PageResult<Author>> listAuthors(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        return Response.ok(new PageResult<Author>(authorService.listAuthors(page, size)));
    }
}
